<?php

// No direct access to this file
defined('_JEXEC') or die('Restricted access');

class BakerysModelCart extends JModelItem
{
	function storeCart()
	{
		$db = JFactory::getDbo();
		$input = JFactory::getApplication()->input;
		//$cart = $input->get("cart");
		$cart = JRequest::getVar("cart");
		$action = JRequest::getVar("action");
		$location_id = $input->get("location");
		$location_alias = $input->get("location_alias");
		
		if(empty($cart))
		{
			$session = JFactory::getSession();
			$cart = $session->get("bakery_cart_".$location_id);
			if(isset($cart)) return $cart;
			else 
			{
				$app = JFactory::getApplication();
				$link = JRoute::_("index.php?option=com_bakerys&view=bakerys&location=".$location_id.":".$location_alias);
				$app->redirect($link);
			}
		}
		$arr = json_decode($cart);
		$arr_check = json_decode($cart,true);
		if(count($arr_check) == 0)
		{
			$session = JFactory::getSession();
			$session->clear("bakery_cart_".$location_id);
			$app = JFactory::getApplication();
			$link = JRoute::_("index.php?option=com_bakerys&view=bakerys&location=".$location_id.":".$location_alias);
			$app->redirect($link);
		}
		
		
		$arr_itemid = array();
		foreach ($arr as $key=>$value)
		{
			list($str,$itemid) = explode("_",$key); 
			$arr_itemid[] = $itemid;
		}
		
		$query = $db->getQuery(true);
		$query->select("i.*,li.price as location_price")
			->from("#__bakery_items i")
			->leftJoin("#__bakery_location_items li on li.item_id = i.id and location_id = $location_id")
			->where("i.state = 1 and i.id in (".implode(",", $arr_itemid).")");
		$db->setQuery($query);
		$items = $db->loadObjectList();
		$arr_cart = array();
		foreach ($items as $item)
		{
			foreach($arr as $cart_item)
			{
				if($cart_item->itemid == $item->id)
				{
					$arr_tmp = array();
					$arr_tmp["productname"] = $item->title;
					$arr_tmp["productid"] = $item->id;
					if(empty($item->location_price))
					{
						$arr_tmp["price"] = $item->price;
					}else $arr_tmp["price"] = $item->location_price;
					$arr_tmp["quantity"] = $cart_item->quantity;
					$arr_cart[] = $arr_tmp;
				}
			}
		}
		
		$session = JFactory::getSession();
		$session->set("bakery_cart_".$location_id,$arr_cart);
		return $arr_cart;
	}
	
	function checkout()
	{
		$products = JRequest::getVar("productid");
		$session = JFactory::getSession();
		$cart = $session->get("sct_cart");
		
		$customer_id = $this->insertCustomer();
		$created_date = date("Y-m-d");
		$status = "pending";
		$query = "insert into #__plg_buybutton_order values(NULL,$customer_id,'$created_date','$status',0)";
		$db = JFactory::getDbo();
		$db->setQuery($query);
		$db->query();
		$query = "SELECT LAST_INSERT_ID()";
		$db->setQuery($query);		
		$order_id = $db->loadResult();
		$params = JComponentHelper::getParams("com_sascontent");

		$query = "insert into #__plg_buybutton_order_detail values ";
		$subqueries = array();
		foreach($products as $product)
		{
			list($appid,$itemid) = explode("_",$product);
			if(isset($cart[$appid][$itemid]))
			{
				$quantity = JRequest::getVar("quantity_$product");
				$price = $cart[$appid][$itemid]["price"];
				$productname = $cart[$appid][$itemid]["productname"];
				$currency_country = $cart[$appid][$itemid]["currency_country"];
				$currency_rate = $cart[$appid][$itemid]["currency_rate"];
				$subqueries[] = "(NULL,$order_id,$appid,$itemid,$quantity,$price,'$productname',$currency_rate,'$currency_country')";  
			}
		}
		if(count($subqueries) > 0)
		{
			$query .= implode(",",$subqueries);
			$db->setQuery($query);
			$db->query();
		}
	}
	
	
	
	function insertCustomer()
	{
		$fullname = JRequest::getVar("fullname");
		$phone = JRequest::getVar("phone");
		$address = JRequest::getVar("address");
		$db = JFactory::getDbo();
		$query = "insert into #__plg_buybutton_customer values(NULL,'$fullname','$phone','$address')";
	
		$db->setQuery($query);
		$db->query();
		$query = "SELECT LAST_INSERT_ID()";
		$db->setQuery($query);		
		$id = $db->loadResult();
		return $id;
	}
}
